﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using bzxxxt.admin.entity;
using bzxxxt.dal;
namespace bzxxxt.admin.service
{
    public class ButtonService : BaseService
    {
        public string getbutton(int userId, string menucode, string pagename)
        {
            var roleIds = DB.tbUser.Single(m => m.id == userId).tbRole.Select(m => m.id).ToList();
            var menu = DB.tbMenu.Single(m => m.code == menucode);
            var buttons = menu.tbRoleMenuButton.Where(m => roleIds.Contains(m.roleId)).Select(m => m.tbButton).Distinct().OrderBy(m=>m.sort);
            
            var toolbars = buttons.Where(m=>m.code!="browser").Select(m => new ToolBarEntity { 
                    text = m.name,
                    iconCls = m.icon,
                    handler = pagename+"_"+m.code+"();"
                });
            bool browser = false;
            if (buttons.FirstOrDefault(m => m.code == "browser") != null) browser = true;
            return JsonUtil.ToJson(new { success = true, browser = browser, toolbar = toolbars });
        }
        public string getall()
        {
            var list = from b in DB.tbButton
                       where b.id>1
                       orderby b.sort
                       select new ButtonEntity
                       {
                           id = b.id,
                           text = b.name,
                           code = b.code,
                           sort = b.sort,
                           iconCls = b.icon,
                           description = b.description,
                           addDate = b.addDate
                       };
            string json = JsonUtil.ToJson(list.ToList());
            return json;
        }

        public string search(int pageSize, int pageIndex, string order, string where) {
            int totalCount = 0;
            where = "id>1 and " + where;
            SqlParameter paramTotalCount = new SqlParameter("@totalCount", totalCount);
            paramTotalCount.Direction = ParameterDirection.Output;
            var pager = DB.tbButton.SqlQuery("exec sp_Pager @tableName,@columns,@order,@pageSize,@pageIndex,@where,@totalCount output",
                GetPagerParameters("tbButton","*",order,where,pageSize,pageIndex,paramTotalCount)).ToList();
            var list = from b in pager
                       orderby b.sort
                       select new ButtonEntity
                       {
                           id = b.id,
                           text = b.name,
                           code = b.code,
                           sort = b.sort,
                           iconCls = b.icon,
                           description = b.description ?? "",
                           addDate = b.addDate
                       };
            var l = list.ToList();
            totalCount = Convert.ToInt32(paramTotalCount.Value);
            string json = JsonUtil.ToJson(new { total = totalCount, rows = l });
            return json;
        }

        public string add(tbButton b) {
            DB.tbButton.Add(b);
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success=true });
            return json;
        }

        public string edit(tbButton b)
        {
            var old = DB.tbButton.FirstOrDefault(m => m.id == b.id);
            old.name = b.name;
            old.sort = b.sort;
            old.icon = b.icon;
            old.code = b.code;
            old.description = b.description;
            
            DB.SaveChanges();
            string json = JsonUtil.ToJson(new { success = true });
            return json;
        }

        public string delete(string strIds) {
            if (string.IsNullOrEmpty(strIds)) {
                return JsonUtil.ToJson(new { success=false,msg = "没有选择要删除的记录！"});
            }
            string[] arr = strIds.Split(',');
            List<int> ids= new List<int>();
            foreach (var id in arr) {
                ids.Add(int.Parse(id));
            }
            var buttons = DB.tbButton.Where(m=>ids.Contains(m.id)).ToList();
            if (buttons != null)
            {
                buttons.ForEach(m => DB.tbButton.Remove(m));
                DB.SaveChanges();
                return JsonUtil.ToJson(new { success = true });
            }
            else {
                return JsonUtil.ToJson(new { success = false,msg="删除失败！" });
            }
        }
        
    }
}